﻿using System.IO;
using Common.Utils.Helper;
using Game.Kernel;
using UnityEngine;

namespace Game.Logic.Data
{
    public class GameVersionData : GameBaseData
    {
        private CSharpLuaTable m_versionInfo;
        public CSharpLuaTable VersionInfo { get => m_versionInfo; }

        private string m_path;
        public string Path { get => m_path; }

        private int m_main;
        public int Main { get => m_main; set => m_main = value; }

        private int m_sub;
        public int Sub { get => m_sub; set => m_sub = value; }

        private int m_phase;
        public int Phase { get => m_phase; set => m_phase = value; }

        private string m_date;
        public string Date { get => m_date; set => m_date = value; }

        private string m_type;
        public string Type { get => m_type; set => m_type = value; }

        private string m_ver;
        public string Ver { get => m_ver; set => m_ver = value; }

        public static GameVersionData ParseFile(string _path)
        {
            if (!File.Exists(_path))
                return null;

            GameVersionData rt = new GameVersionData();
            rt.SetDataType(GameDataType.GameVersion);
            rt.m_path = _path;
            Debug.LogFormat("Load game version data config! path-->{0}", _path);
            rt.SetVersionData(CSharpLuaTable.ParseFromFileEx(_path));
            return rt;
        }


        public void SetVersionData(GameVersionData _value)
        {
            SetVersionData(_value.VersionInfo);
        }


        public void SetVersionData(CSharpLuaTable _versionInfo)
        {
            m_versionInfo = _versionInfo;
            SetVersion(VersionInfo.GetField("version"));
            Date = VersionInfo.GetField("date");
            Type = VersionInfo.GetField("type");
        }


        public void SetVersion(string _version)
        {
            string[] arrVersion = _version.Split('.');
            if (arrVersion.Length < 3)
                return;

            string strMain = arrVersion[0];
            Main = System.Convert.ToInt32(strMain);

            string strSub = arrVersion[1];
            Sub = System.Convert.ToInt32(strSub);

            string strPhase = arrVersion[2];
            Phase = System.Convert.ToInt32(strPhase);

            Ver = _version;
            VersionInfo.SetField("version", _version);
        }


        public void SetVersion(int _main, int _sub, int _phase)
        {
            Main = _main;
            Sub = _sub;
            Phase = _phase;

            string version = string.Format("{0}.{1}.{2}", Main, Sub, Phase);

            Ver = version;
            VersionInfo.SetField("version", version);
        }


        public string GetVersionStr()
        {
            return string.Format("{0}.{1}.{2}.{3}.{4}", Main, Sub, Phase, Date, Type);
        }
    }
}
